<?php

namespace app\api\controller\v2;

use app\admin\model\cms\Channels;
use app\admin\model\cms\MembershipExpiryRecordModel;
use app\admin\model\cms\PlazaCategorymodel;
use app\admin\model\cms\PlazaCommentModel;
use app\admin\model\cms\PlazaPostModel;
use app\admin\model\User as UserModel;
use app\admin\model\rotation\chart\Management;
use app\admin\model\UserGroup;
use app\admin\model\UserRule;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\UserLike;
use app\common\model\UserForward;
use app\common\model\UserFavorite;
use app\common\model\UserView;
use app\common\model\Comment;
use app\admin\model\cms\Platepost;
use fast\Random;
use think\Cache;
use think\Config;
use think\Validate;

/**
 * 会员到期
 */
class MembershipExpiryRecord extends Api
{
    //无需登录的方法,同时也就不需要鉴权了
    protected $noNeedLogin = '*';
    //无需鉴权的方法,但需要登录
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 判断会员是否到期
     * @param $user_id 用户id
     * @param $goods_type 商品类型|categories:栏目,articles:文章
     * @param $table_name 商品来源的表名|不带表名前缀
     * @param $goods_id 商品相关id|table_name表名下的id值
     */
    public function isMemberExpire($user_id,$goods_type,$table_name,$goods_id){
        $time = time();
        $MembershipExpiryRecord = MembershipExpiryRecordModel::where('user_id',$user_id)
            ->where('goods_type',$goods_type)
            ->where('table_name',$table_name)
            ->where('goods_id',$goods_id)
            ->order('id','desc')
            ->find();
        if ($MembershipExpiryRecord) {
            //expiration_time(到期时间戳) == 0//无限天
            if ($MembershipExpiryRecord['expiration_time'] == 0) {
                return [
                    'code' => 200,
                    'msg' => '已开通会员'
                ];
            } elseif ($MembershipExpiryRecord['expiration_time'] >= $time) {
                return [
                    'code' => 200,
                    'msg' => '已开通会员'
                ];
            }
        }
        return [
            'code'=>500,
            'msg'=>'尚未开通会员'
        ];
    }

}
